package cn.mrlong.mdialogstyle.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.mrlong.mdialogstyle.R;

public class AlertSheetDialog {
    private Context context;
    private TextView tv_cancel;
    private TextView tv_title;
    private boolean isShowTitle;
    private Dialog dialog;
    private RecyclerView rv_items;
    private SheetAdapter adapter;
    private ImageView iv_titleline;

    public AlertSheetDialog(Context context) {
        this.context = context;
    }

    public AlertSheetDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_actionsheet, null);

        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dialog) {
                    dialog.dismiss();
                }
            }
        });
        tv_title = view.findViewById(R.id.tv_title);
        rv_items = view.findViewById(R.id.rv_items);
        iv_titleline = view.findViewById(R.id.iv_titleline);
        initRecycelerView(context);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        view.setMinimumWidth(display.getWidth());
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        lp.y = 0;
        window.setAttributes(lp);
        return this;
    }

    public AlertSheetDialog setItemClickListener(OnSheetItemClickListener listener) {
        adapter.setClickListener(listener);
        return this;
    }

    private void initRecycelerView(Context context) {
        rv_items.setLayoutManager(new LinearLayoutManager(context));
        List<String> datas = new ArrayList<>();
        adapter = new SheetAdapter(context, datas);
        rv_items.setAdapter(adapter);
    }

    class SheetAdapter extends RecyclerView.Adapter<SheetAdapter.SheetHolder> {
        private Context context;
        private List<String> datas;
        private OnSheetItemClickListener itemClickListener;
        private int color;

        public SheetAdapter(Context context, List<String> datas) {
            this.context = context;
            this.datas = datas;

        }

        @Override
        public SheetHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
            SheetHolder holder = new SheetHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(SheetHolder holder, int position) {
            holder.setData(position);
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        public void setClickListener(OnSheetItemClickListener listener) {
            this.itemClickListener = listener;
        }

        public void setItemTextColor(int color) {
            this.color = color;
        }

        class SheetHolder extends RecyclerView.ViewHolder {
            int position;
            TextView tv_content;
            ImageView line;

            public SheetHolder(View itemView) {
                super(itemView);
                tv_content = itemView.findViewById(R.id.tv_content);
                                line = itemView.findViewById(R.id.img_line);
            }

            public void setData(final int position) {
                this.position = position;
                tv_content.setText(datas.get(position));
                if (color != 0) {
                    tv_content.setTextColor(color);
                }
                if (position == datas.size() - 1) {
                    line.setVisibility(View.GONE);
                } else {
                    line.setVisibility(View.VISIBLE);
                }
                tv_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        itemClickListener.onClick(position);
                        if (null != dialog) {
                            dialog.dismiss();
                        }
                    }
                });
            }

        }
    }

    /**
     * 点击返回键是否消失
     *
     * @param cancel
     * @return
     */
    public AlertSheetDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }


    /**
     * 添加item
     *
     * @param strItem
     * @return
     */
    public AlertSheetDialog addItem(List<String> strItem) {
        adapter.datas.clear();
        adapter.datas.addAll(strItem);
        adapter.notifyDataSetChanged();
        return this;
    }

    public interface OnSheetItemClickListener {
        void onClick(int which);
    }

    /**
     * 显示dialog
     */
    public void show() {
        showViewControl();
        dialog.show();
    }

    private void showViewControl() {
        if (isShowTitle) {
            tv_title.setVisibility(View.VISIBLE);
        } else {
            tv_title.setVisibility(View.GONE);
        }
    }

    /**
     * 设置字体颜色
     */
    public AlertSheetDialog setTitleTextColor(int color) {
        tv_title.setTextColor(color);
        return this;
    }

    /**
     * 设置字体颜色
     */
    public AlertSheetDialog setItemTextColor(int color) {
        adapter.setItemTextColor(color);
        return this;
    }


    /**
     * 点击空白区域是否消失
     *
     * @param cancel
     * @return
     */
    public AlertSheetDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    /**
     * 是否显示标题
     *
     * @param title
     * @return
     */
    public AlertSheetDialog setTitle(String title) {
        isShowTitle = true;
        if (null != title && !"".equals(title)) {
            tv_title.setText(title);
            iv_titleline.setVisibility(View.VISIBLE);
            tv_title.setVisibility(View.VISIBLE);
        } else {
            iv_titleline.setVisibility(View.GONE);
            tv_title.setVisibility(View.GONE);
        }
        return this;
    }

}
